#define _CRT_SECURE_NO_WARNINGS #include #include #include typedef struct { char name[100]; } element; typedef struct ListNode { element data; struct ListNode* link; } ListNode; element delete_first(ListNode* head) { ListNode* removed; element data; if (head == (ListNode*)NULL) return; removed = head->link; data = removed->data; if (head == removed) head = NULL; else head->link = removed->link; free(removed); return data; } ListNode* insert_last(ListNode* head, element data) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->data = data; if (head == NULL) { head = node; node->link = head; } else { node->link = head->link; head->link = node; head = node; } return head; } // 리스트의 항목 출력 void print_list(ListNode* head) { ListNode* p; if (head == NULL) return; // 데이터가 없을 때 if (head == head->link) { // 데이터가 1개 일 때 printf("%s->\n", head->data); } else { // 데이터가 2개 이상일 때 p = head->link; do { printf("%s->", p->data); p = p->link; } while (p != head); printf("%s->\n", p->data); // 마지막 노드 출력 } } int main(void) { ListNode* head = NULL; element data; strcpy(data.name, "KIM"); head = insert_last(head, data); print_list(head); strcpy(data.name, "PARK"); head = insert_last(head, data); print_list(head); strcpy(data.name, "CHOI"); head = insert_last(head, data); print_list(head); data = delete_first(head); printf("%s\n", data.name); data = delete_first(head); printf("%s\n", data.name); data = delete_first(head); printf("%s\n", data.name); return 0; }